package com.gec.system.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gec.model.entity.SysCommunity;
import com.gec.model.vo.SysCommunityQueryVo;
import com.gec.system.service.SysCommunityService;
import com.gec.system.util.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "小区管理控制器")
//登录注册控制器
@RestController
@RequestMapping("/admin/system/sysCommunity")
public class SysCommunityController {

    @Autowired
    private SysCommunityService sysCommunityService;


    // 分页和条件查询
    @ApiOperation("分页和条件查询")
    @GetMapping("/{page}/{limit}")
    public Result selectCommunityPageVo(@PathVariable Long page,
                                        @PathVariable Long limit,
                                        SysCommunityQueryVo sysCommunityQueryVo
                                        ){
        IPage<SysCommunity> iPage =new Page<>(page,limit);

        System.out.println("ipage条件："+iPage);

        iPage=this.sysCommunityService.selectPage(iPage,sysCommunityQueryVo);

        System.out.println("控制层打印小区信息："+iPage);
        return Result.ok(iPage);
    }
    //添加小区
    @ApiOperation("添加小区")
    @PostMapping("/addCommunity")
    public Result addCommunity(@RequestBody SysCommunity sysCommunity){

        String code = this.sysCommunityService.getCode();
        System.out.println(code);
        sysCommunity.setCode(code);
        boolean save = this.sysCommunityService.save(sysCommunity);
        return save ? Result.ok() : Result.fail();
    }

    //删除单个小区
    @ApiOperation("删除小区")
    @GetMapping("/removeCommunityById/{id}")
    public Result removeCommunityById(@PathVariable Long id){
        boolean b = this.sysCommunityService.removeById(id);
        return b ? Result.ok():Result.fail();
    }

    //删除多个小区
    @ApiOperation("删除多个小区")
    @DeleteMapping("/removeCommunityByIds")
    public Result removeCommunityByIds(@RequestBody List<Long> ids){
        boolean b = this.sysCommunityService.removeByIds(ids);
        return b ? Result.ok():Result.fail();
    }

    //根据id查询当前要修改的小区
    @ApiOperation("根据id查询当前要修改的小区")
    @GetMapping("/finCommunityById/{id}")
    public Result finCommunityById(@PathVariable Long id){
        SysCommunity byId = this.sysCommunityService.getById(id);
        return Result.ok(byId);
    }
    //修改
    @ApiOperation("根据id修改当前小区")
    @PostMapping("/updateCommunity")
    public Result updateCommunity(@RequestBody SysCommunity sysCommunity){
        boolean b = this.sysCommunityService.updateById(sysCommunity);
        return b ? Result.ok():Result.fail();
    }
    //查询所有小区

    @ApiOperation("查询所有小区")
    @GetMapping("/finAllCommunity")
    public Result finAllCommunity(){
        List<SysCommunity> list = this.sysCommunityService.list();
        return Result.ok(list);
    }
}
